const fs = require('fs')

//求三个数最大值
const getThreeMax = (m, n, k) => {
	let max = m
	if (n > max) {
		max = n
	}
	if (k > max) {
		max = k
	}
	return max
}

/**
 * 使用promise封装一个异步读文件的工具方法
 */

const readFilePromise = fileUrl => {
	const promise = new Promise((resolve, reject) => {
		//封装异步读文件
		fs.readFile(fileUrl, 'utf-8', function (err, data) {
			if (err) {
				reject(err) //失败结果
				return
			}
			resolve(data.toString()) // 处理成功结果
		})
	})

	return promise
}

//2.写文件封装一个函数 writeFilePromise
const writeFilePromise = function (fileUrl, content) {
	const promise = new Promise(function (resolve, reject) {
		fs.writeFile(fileUrl, content, function (err) {
			if (err) {
				reject('写文件出错!')
			} else {
				resolve('写文件成功!')
			}
		})
	})
	return promise
}

module.exports = {
	getThreeMax: getThreeMax,
	readFilePromise: readFilePromise,
	writeFilePromise: writeFilePromise,
}
